//
//  DFPicturesVC.m
//  DFront
//
//  Created by lanou on 15/10/30.
//  Copyright © 2015年 xuzenghui. All rights reserved.
//

#import "DFPicturesVC.h"
#import "DFPictureModel.h"
#import "PKRequestManager.h"
#import "DFpictureCell.h"
#import "DFClickAppleViewController.h"
#import "MJRefresh.h"
@interface DFPicturesVC ()
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@property(nonatomic,strong)DFPictureModel *model;
@property(nonatomic,assign)int page;

@end

@implementation DFPicturesVC

- (void)viewDidLoad {
    [super viewDidLoad];

    _dataSourceArray = [NSMutableArray array];
    [self.tableView registerClass:[DFpictureCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.rowHeight = 262;
    [self requestDataWithType:0];
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDataWithType:0];
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self requestDataWithType:1];
    }];

}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataSourceArray.count;
}

-(void)requestDataWithType:(NSInteger)type
{
    if (type == 0) {
        _page = 1;
    }
    else
    {
        _page ++;
    }
    NSString *str = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/ipj/tushang.php?&page=%d&retina=1&vs=iph38",_page];
    
    [PKRequestManager requestWithType:GET urlString:str parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        if (type == 0) {
            [self.dataSourceArray removeAllObjects];
        }
        NSArray *listArray = dic[@"list"];
        for (NSDictionary *modelDic in listArray) {
            _model = [[DFPictureModel alloc]init];
            [_model setValuesForKeysWithDictionary:modelDic];
            [_dataSourceArray addObject:_model];
            
        }
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
    
    
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DFpictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.ConfigureModel = _dataSourceArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DFPictureModel *model = _dataSourceArray[indexPath.row];
    DFClickAppleViewController *picWeb = [[DFClickAppleViewController alloc] init];
    picWeb.str = model.url;
    [self.navigationController pushViewController:picWeb animated:YES];
}

@end
